﻿using Sc.Domain;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Sc.Service
{
    public class CommService
    {
        /// <summary>
        /// 判断当前用户是否能审批或者已经审批
        /// </summary>
        /// <param name="app">审批人数据</param>
        /// <param name="userId">当前用户</param>
        /// <param name="nodeCount">流程节点数</param>
        /// <returns></returns>
        public static int IsApproval(string app, int userId, int nodeCount)
        {
            var tmp = app.Split(';');
            var curNode = tmp[tmp.Length - nodeCount-1]; var node = curNode.Split(',');
            return node.Where(it => it == "USER" + userId).ToList().Count;
        }
        /// <summary>
        /// 将当前用户更新为已审核
        /// </summary>
        /// <param name="app">审批人字符串</param>
        /// <param name="userId">当前用户Id</param>
        /// <param name="nodeCount">流程节点数</param>
        /// <returns></returns>
        public static string UpdateApproalUers(string app, int userId, int nodeCount)
        {
            var tmp = app.Split(';');
            var curNode = tmp[tmp.Length - 1 - nodeCount];
            curNode = curNode.Replace("USER" + userId, userId.ToString());
            tmp[tmp.Length - 1 - nodeCount] = curNode;
            var approalUsers = string.Empty;
            for (int i = 0; i < tmp.Length; i++)
            {
                if (i > 0) approalUsers += ";";
                approalUsers += tmp[i];
            }
            return approalUsers;
        }
        /// <summary>
        /// 判断是否更新节点
        /// </summary>
        /// <param name="app">审批人字符串</param>
        /// <param name="userId">当前用户Id</param>
        /// <param name="nodeCount">流程节点数</param>
        /// <returns></returns>
        public static int IsUpdateNodeCount(string app, int userId, int nodeCount)
        {
            var tmp = app.Split(';');
            var curNode = tmp[tmp.Length - 1 - nodeCount];
            curNode = curNode.Replace("USER" + userId, userId.ToString());
            var mp = curNode.Split(',');
            var temp = mp.Where(it => it.Contains("USER")).ToList().Count;
            return temp > 0 ? nodeCount : nodeCount - 1;
        }
        /// <summary>
        /// 找到下一个节点用户
        /// </summary>
        /// <param name="app">审批人数据</param>
        /// <param name="userId">当前用户</param>
        /// <param name="nodeCount">流程节点数</param>
        /// <returns></returns>
        public static IList<int> FindNextUsers(string app, int userId, int nodeCount)
        {
            var tmp = app.Split(';');
            var nextNode = tmp.Length - 1 - IsUpdateNodeCount(app, userId, nodeCount);
            if (tmp.Length >= nextNode)
            {
                IList<int> list = new List<int>();
                var us = tmp[nextNode].Split(',');
                foreach (var item in us)
                {
                    Sc.Domain.RefDataRecord rec = new Sc.Domain.RefDataRecord(item);
                    list.Add(rec.ObjectId);
                }
                return list;
            }
            return null;
        }
        /// <summary>
        /// 新建webservice实例
        /// </summary>
        /// <returns></returns>
        public static Sys.SysWebservice IntanceSys()
        {
            Sys.SysWebservice sys = new Sys.SysWebservice();
            sys.Url = ConfigurationManager.AppSettings["webUrl"];
            return sys;
        }
        /// <summary>
        /// 根据用户权限获取用户权限范围
        /// </summary>
        /// <param name="userId">用户ID</param>
        /// <param name="perssionId">权限ID</param>
        /// <returns></returns>
        public static IList<CommOrgz> GetOrgzByUserPer(int userId,int perssionId)
        {
            Sys.SysWebservice opa = IntanceSys();
            var ent = opa.LoadUserPerimissionsById(new Sys.Users() { Id = userId }, perssionId);
            var sc = ent.PermissionList.FirstOrDefault().ScopeObj.Split(',');
            var sysNo = Convert.ToInt32(ConfigurationManager.AppSettings["SysNo"]);
            var olist = opa.GetAllOrgzBySysNo(sysNo);
            var data = from a in sc
                       join b in olist on a equals "ORGZ" + b.Id
                       select new CommOrgz()
                       {
                           Id = b.Id,
                           OrgzName = b.Name
                       };
            return data.ToList();
        }
    }
}
